package com.lanou.beautifulwardrobe.activity_zhuti.adapter;

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.view.TimeManager;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.ProductActivity;
import com.lanou.beautifulwardrobe.listener_interface.MyHotItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class ActivityRecyclerViewAdapter extends RecyclerView.Adapter<ActivityRecyclerViewAdapter.ActivityViewHolder> {

    private List<ProductActivity> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private MyHotItemClickListener listener;
    private ImageLoader imageLoader;

    public void setOnMyHotItemClickListener(MyHotItemClickListener listener) {
        this.listener = listener;
    }


    public void addData(List<ProductActivity> productActivities) {
        datas = productActivities;
        notifyDataSetChanged();

    }

    @Override
    public ActivityViewHolder onCreateViewHolder(ViewGroup view, int viewType) {
        View v = LayoutInflater.from(view.getContext()).inflate(R.layout.activity_product_item, null);
        return new ActivityViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ActivityViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {
            ProductActivity productActivity = datas.get(position);
            holder.position=position;
            String imageUrl = productActivity.getPic_url();
            String title = productActivity.getTitle();
            String visits = productActivity.getVisits_count();
            String likes = productActivity.getLikes_count();
            String startTimeStr = productActivity.getPublished_time();//getStart_time();
            long startTime = Long.parseLong(startTimeStr);
            long endTime = Long.parseLong(productActivity.getEnd_time());
            holder.activity_productTitle.setText(title);
            holder.activity_productLooks.setText(visits);
            if (likes != null) {
                holder.activity_productLikes.setText(likes);
            } else {
                holder.activity_productLikes.setText("0");
            }

            holder.stratTime.setText(TimeManager.timeStamp2Date(startTimeStr, "yyyy年MM月dd日 HH时"));
            long current = Long.parseLong(TimeManager.timeStamp());

            getBitMap(imageUrl, holder.activity_productImage);
        }

    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }


    class ActivityViewHolder extends RecyclerView.ViewHolder {
        private NetworkImageView activity_productImage;
        private TextView activity_productTitle;
        private TextView activity_productLooks;
        private TextView activity_productLikes;
        private TextView stratTime;
        private CardView cardview;
        private int position;

        public ActivityViewHolder(View view) {
            super(view);
            activity_productImage = (NetworkImageView) view.findViewById(R.id.activity_productImage);
            activity_productTitle = (TextView) view.findViewById(R.id.activity_productTitle);
            activity_productLooks = (TextView) view.findViewById(R.id.activity_productLooks);
            activity_productLikes = (TextView) view.findViewById(R.id.activity_productLikes);
            stratTime = (TextView) view.findViewById(R.id.stratTime);
            cardview= (CardView) view.findViewById(R.id.cardview);
            cardview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (datas != null && datas.size() > 0) {
                        String url = datas.get(position).getUrl();
                        String title = datas.get(position).getShort_title();
                        String activity_type = datas.get(position).getContent().getActivity_type();
                        String ids = datas.get(position).getId();
                        if (listener != null) {
                            listener.onMyHotItemClickListener(url, title, activity_type,ids);

                        }
                    }
                }
            });
        }
    }

    /** 拉取图片 **/
    private void getBitMap(String url, NetworkImageView imageView){
        imageLoader = volleySingleton.getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);
    }
}
